package com.yunyao.dao.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yunyao.common.core.BaseContext;
import com.yunyao.common.core.UserInfoRequest;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author xingjishuai
 * @date 2024-07-24
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        setFieldValByName("createdAt", new Date(), metaObject);
        setFieldValByName("updatedAt", new Date(), metaObject);
        setFieldValByName("createdBy", BaseContext.getUserId(), metaObject);
        setFieldValByName("updatedBy", BaseContext.getUserId(), metaObject);
        UserInfoRequest userInfo = BaseContext.getUserInfo();
        setFieldValByName("createDept", userInfo.getDeptId(), metaObject);
        setFieldValByName("tenantId", userInfo.getTenantId(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updatedAt", new Date(), metaObject);
        setFieldValByName("updatedBy", BaseContext.getUserId(), metaObject);
    }
}
